﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public enum GameStatus
{
    ConnectPlatformSucess = 0,
    AuthSucced,
    ConnectorSucess,
    Login,
    InRoom,
    GameStart,
    GameOver,
    Disconnect,
    TotalLevel,
    Last,
}
public class GlobeCount : MonoBehaviour {

	// Use this for initialization
    public Text mText;
    public Text mShowQps;
    public int[] Counts= new int[8];
    public int howMuch = 200;
    public int dead = 0;
    public class DataCount{
        public double max = 0;
        public double min = 999999;
        public int count = 0;
        public double total = 0;
    }
    public Dictionary<string,DataCount> handle2Count= new Dictionary<string, DataCount>();

    public static GlobeCount Instance;

    public void Click()
    {
        int t = howMuch;
        StartCoroutine(CreateItems(t));
        //while(count-- > 0)
        //Invoke("Click", 1f);
    }

    IEnumerator CreateItems(int t)
    {
        while (t-- > 0)
        {
            int c = 1;
            while (c-- > 0)
            {
                GameObject nobj = new GameObject();
                //niuniu.host = "119.23.147.18";
                //niuniu.port = 8621;
                // robot.host = "119.23.153.59";
                //red.host = "119.23.147.18";
                //red.port = 8221;
                //goldniuniu.port = 8221;
                //red.host = "39.108.213.68";
                //red.host = "116.211.167.103";
                //red.port = 8821;
                //var ret = nobj.AddComponent<red>();
                //bairen.host = "119.23.147.18";
                //bairen.port = 9921;
                var ret = nobj.AddComponent<bairen>();
                ret.CreateGame();
            }
            yield return null;
            yield return null;
            yield return null;
            yield return null;
            yield return null;
            yield return null;
            yield return null;
            yield return null;
            yield return null;
            yield return null; yield return null; yield return null;
            yield return null;
            yield return null;
            yield return null;
            yield return null;
        }
    }
    void Start () {
        Application.targetFrameRate = 30;
        Screen.SetResolution(800, 400, false);
        Instance = this;
        Counts = new int[(int)GameStatus.Last];
	}
    public void AddCount(GameStatus id)
    {
        Counts[(int)id]++;
    }
    public void AddCount(int id)
    {
       Counts[id] ++;
    }

    public void CountQPS(string handler, double qps)
    {
        if(handle2Count.ContainsKey(handler) == false)
        {
            handle2Count.Add(handler, new DataCount());
        }

        DataCount data = handle2Count[handler];
        if(data.min >qps)
        {
            data.min = qps;
        }
        else if (data.max < qps)
        {
            data.max = qps;
        }
        data.count++;
        data.total += qps;
    }
	// Update is called once per frame
	void Update () {
        
        string str = "";
        for(int i=0; i<Counts.Length; i++)
        {
            str +=  ((GameStatus)(i)).ToString() +"  " + Counts[i] +" \n";
        }
        str += "obj  " + (howMuch-dead)+"/"+howMuch + " \n";
        mText.text = str;

        string qps = "";
        foreach (var item in handle2Count)
        {
            qps += item.Key + " " + item.Value.min.ToString("0") + " " + item.Value.max.ToString("0") + " " +
                (item.Value.total / item.Value.count).ToString("0") + " " + item.Value.count + "\n"; 
        }
        mShowQps.text = qps;
	}
}
